home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / intrvews / xgrab.lha / xgrab / ui / GC / interface.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-25  |  2.5 KB  |  94 lines

  1. #include "gc.h"
  2. /* These are some additional routines to interface the collector to C     */
  3. /* They were contributed by David Chase (chase@orc.olivetti.com)          */
  4. /* They illustrates the use of non_gc_bytes, and provide an interface to  */
  5. /* the storage allocator's size information.  Note that there is a        */
  6. /* check to guard against 0 length allocations.                           */
  7. /* Hacked by H. Boehm (11/16/89) to accomodate gc_realloc.                */
  8.  
  9. initialize_allocator() {
  10.   non_gc_bytes = 0;
  11.   gc_init();
  12. }
  13.  
  14.  
  15. /* Use of gc_gasp to report errors reduces risk of bizarre
  16.    interactions with I/O system in desperate situations.  */
  17.  
  18. gc_gasp(s) char * s;
  19. {
  20.   write(2,s,strlen(s));
  21. }
  22.  
  23.  
  24. /* This reports how many bytes are actually available to an object.
  25.    It is a fatal error to request the size of memory addressed by a
  26.    pointer not obtained from the storage allocator. */
  27.  
  28. size_of_obj_in_bytes(p)
  29.      struct obj * p;
  30. {
  31.   register struct hblk * h;
  32.   register int size;
  33.   
  34.   h = HBLKPTR(p);
  35.   
  36.   if (is_hblk(h)) {
  37.     return (HB_SIZE(h))<<2;
  38.   }
  39.   gc_gasp("GC/size_of_obj_in_bytes: requested byte size of non-pointer!\n");
  40.   exit(1);
  41. }
  42.  
  43.  
  44. /* This free routine is merely advisory -- it reduces the estimate of
  45.    storage that won't be reclaimed in the next collection, thus
  46.    making it more likely that the collector will run next time more
  47.    memory is needed. */
  48.  
  49. void free(p) {
  50.   int inc = size_of_obj_in_bytes(p);
  51.   non_gc_bytes -= inc;
  52. }
  53.  
  54. /* This free routine adjusts the collector estimates of space in use,
  55.    but also actually releases the memory for reuse.  It is thus "unsafe"
  56.    if the programmer "frees" memory that is actually still in use.  */
  57.  
  58. void unsafe_free(p) {
  59.   int inc = size_of_obj_in_bytes(p);
  60.   non_gc_bytes -= inc;
  61.   gc_free(p);
  62. }
  63.  
  64.  
  65. /* malloc and malloc_atomic are obvious substitutes for the C library
  66.    malloc.  Note that the storage so allocated is regarded as not likely
  67.    to be reclaimed by the collector (until explicitly freed), and thus
  68.    its size is added to non_gc_bytes.
  69. */
  70.  
  71. word malloc(bytesize) {
  72. word result;
  73. if (bytesize == 0) bytesize = 4;
  74. result = (word) gc_malloc (bytesize);
  75. non_gc_bytes += (bytesize + 3) & ~3;
  76. return result;
  77. }
  78.  
  79. word malloc_atomic(bytesize) {
  80. word result;
  81. if (bytesize == 0) bytesize = 4;
  82. result = (word) gc_malloc_atomic (bytesize);
  83. non_gc_bytes += (bytesize + 3) & ~3;
  84. return result;
  85. }
  86.  
  87. word realloc(old,size) word old,size; {
  88.     int inc = size_of_obj_in_bytes(old);
  89.  
  90.     non_gc_bytes += ((size + 3) & ~3) - inc;
  91.     return(gc_realloc(old, size);
  92.     }
  93.  
  94.